<template>
  <div class="box">
    <h1>组件间通信7: $ref & $children & $parent</h1>

    <p>BABA有存款: {{money}}</p>
    <button @click="borrowMoney1(100)">找小明借钱100</button><br>
    <button @click="borrowMoney2(150)">找小红借钱150</button><br>
    <button @click="borrowMoney3(200)">找所有孩子借钱200</button><br>
    
    <br>
    <Son ref="son"/>

    <br>
    <Daughter ref="dau"/>

  </div>
</template>

<script>
import Son from './Son'
import Daughter from './Daughter'

export default {
  name: 'ChildrenParentTest',
  data () {
    return {
      money: 1000
    }
  },

  methods: {
    borrowMoney1 (num) {
      const son = this.$refs.son
      // son.money -= num // 直接更新子组件的数据
      son.borrowMoney(num)
      this.money += num
    },
    borrowMoney2 (num) {
      const dau = this.$refs.dau
      // dau.money -= num // 直接更新子组件的数据
      dau.borrowMoney(num)
      this.money += num
    },

    borrowMoney3 (num) {
      // 得到所有子组件对象, 遍历更新
      this.$children.forEach(child => {
        child.borrowMoney(num)
        this.money += num
      })
    }
  },

  components: {
    Son,
    Daughter
  }
}
</script>

<style>

</style>
